﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace CBSWeb.Server.App_Start
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseJsonResult
    {
        /// <summary>
        /// 返回有消息无数据的请求成功结果
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static JsonResult Success(string msg)
        {
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.ContractResolver = new NullToEmptyStringContractResolver();
            return new JsonResult(new ApiResult<string> { Status = (int)ResultType.success, Message = msg, Data = null }, jsonSerializerSettings);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static JsonResult Success()
        {
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.ContractResolver = new NullToEmptyStringContractResolver();
            return new JsonResult(new ApiResult<string> { Status = (int)ResultType.success, Message = "操作成功", Data = null }, jsonSerializerSettings);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static JsonResult Success(string msg, Object data)
        {
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.ContractResolver = new NullToEmptyStringContractResolver();
            jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            return new JsonResult(new ApiResult<Object> { Status = (int)ResultType.success, Message = msg, Data = data }, jsonSerializerSettings);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static JsonResult Success(Object data)
        {
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.ContractResolver = new NullToEmptyStringContractResolver();
            jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            return new JsonResult(new ApiResult<Object> { Status = (int)ResultType.success, Message = "操作成功", Data = data }, jsonSerializerSettings);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static JsonResult Error(string msg)
        {
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.ContractResolver = new NullToEmptyStringContractResolver();
            jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            return new JsonResult(new ApiResult<string> { Status = (int)ResultType.error, Message = msg, Data = null }, jsonSerializerSettings);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static JsonResult Error(string msg, Object data)
        {
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.ContractResolver = new NullToEmptyStringContractResolver();
            jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            return new JsonResult(new ApiResult<Object> { Status = (int)ResultType.error, Message = msg, Data = data }, jsonSerializerSettings);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static JsonResult Error()
        {
            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.ContractResolver = new NullToEmptyStringContractResolver();
            jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            return new JsonResult(new ApiResult<string> { Status = (int)ResultType.error, Message = "操作失败", Data = null }, jsonSerializerSettings);
        }
    }
}
